//
//  HXDStatusOriginalView.swift
//  weibo
//
//  Created by huxuedong on 15/12/9.
//  Copyright © 2015年 huxuedong. All rights reserved.
//

import UIKit
import SnapKit

class HXDStatusOriginalView: UIView {

    private lazy var iconImage: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), textSize: nameFontSize)
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), textSize: timeSourceFontSize)
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.lightGrayColor(), textSize: timeSourceFontSize)
    private lazy var vipImage: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    private lazy var verifiedImage: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    private lazy var textLabel: HXDStatusTextLabel = HXDStatusTextLabel(textColor: UIColor.darkGrayColor(), textSize: textFontSize, maxWidth: kScreenW - 2 * margin)
    private lazy var originalPictureView: HXDRetweetedPictureView = {
        let view = HXDRetweetedPictureView()
        return view
    }()
    
    var bottomConstaint: Constraint?
    var statusViewModel: HXDStatusViewModel? {
        didSet {
            iconImage.sd_setImageWithURL(NSURL(string: (statusViewModel?.status?.user?.profile_image_url)!), placeholderImage: UIImage(named: "avatar_default_big"))
            nameLabel.text = statusViewModel?.status?.user?.name
            timeLabel.text = statusViewModel?.createText
            sourceLabel.text = statusViewModel?.sourceText
            vipImage.image = statusViewModel?.vipImage
            verifiedImage.image = statusViewModel?.verifiedImage
            textLabel.attributedText = statusViewModel?.originalStatusAttribute
            textLabel.specialString = statusViewModel?.originalStatusSpecialString
            //移除原创视图当前的底部约束
            bottomConstaint?.uninstall()
            if let pic_urls = statusViewModel?.status?.pic_urls where pic_urls.count > 0 {
                originalPictureView.pic_urls = pic_urls
                originalPictureView.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstaint = make.bottom.equalTo(originalPictureView).offset(margin).constraint
                })
            } else {
                originalPictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstaint = make.bottom.equalTo(textLabel).offset(margin).constraint
                })
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        backgroundColor = UIColor.whiteColor()
        addSubview(iconImage)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(vipImage)
        addSubview(verifiedImage)
        addSubview(textLabel)
        addSubview(originalPictureView)
        iconImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(margin)
            make.leading.equalTo(self).offset(margin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage)
            make.leading.equalTo(iconImage.snp_trailing).offset(margin)
        }
        vipImage.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp_trailing).offset(margin)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconImage)
            make.leading.equalTo(nameLabel)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp_trailing).offset(margin)
        }
        verifiedImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImage.snp_trailing).offset(-3)
            make.centerY.equalTo(iconImage.snp_bottom).offset(-3)
        }
        textLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconImage)
            make.trailing.equalTo(self).offset(-margin)
            make.top.equalTo(iconImage.snp_bottom).offset(margin)
        }
        originalPictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(textLabel)
            make.top.equalTo(textLabel.snp_bottom).offset(margin)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        snp_makeConstraints { (make) -> Void in
            self.bottomConstaint = make.bottom.equalTo(originalPictureView).offset(margin).constraint
        }
    }

}
